package com.lz.sports.ui.config

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.lz.comm.BR
import com.lz.comm.SP
import com.lz.library.base.BaseActivity
import com.lz.library.extension.click
import com.lz.library.extension.resString
import com.lz.library.extension.showToast
import com.lz.library.utils.ActivityScrollObserver
import com.lz.sports.R
import com.lz.sports.core.*
import com.lz.sports.databinding.ActExamRuleBinding
import com.lz.sports.dialog.SelectDialog
import com.lz.sports.vm.config.ExamRuleViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//考试规则
class ExamRuleActivity : BaseActivity<ExamRuleViewModel, ActExamRuleBinding>(), SelectDialog.OnSelectedListener {

    companion object {
        const val REQUEST_MODEL = 1
        const val REQUEST_CONDITION = 2
    }

    override fun getLayoutRes(): Int = R.layout.act_exam_rule

    override fun getVMVariableId(): Int = BR.vm

    override fun initData(savedState: Bundle?) {
        lifecycleScope.launch {
            arrayOf(binding.tvModel, binding.tvCondition, binding.tvSave)
                .click().collect { onClick(it) }
        }
        binding.rgCheck.check(if (SP.getBoolean(K_RULE_EXAM_CHECK, v_ruleExamCheck)) R.id.rb_yes0 else R.id.rb_no0)
        binding.rgResit.check(if (SP.getBoolean(K_RULE_EXAM_RESIT, v_ruleExamResit)) R.id.rb_yes1 else R.id.rb_no1)
        binding.rgTrip.check(if (SP.getBoolean(K_RULE_EXAM_TRIP, v_ruleExamTrip)) R.id.rb_yes2 else R.id.rb_no2)
        binding.rgSave.check(if (SP.getBoolean(K_RULE_EXAM_SAVE, v_ruleExamSave)) R.id.rb_yes3 else R.id.rb_no3)

        ActivityScrollObserver.observe(this, binding.root)
    }

    override fun onSelected(dialog: SelectDialog, position: Int, requestCode: Int) {
        dialog.dismiss()
        when (requestCode) {
            REQUEST_MODEL -> viewModel.selectExamModel(position)
            REQUEST_CONDITION -> viewModel.selectCondition(position)
        }
    }

    private fun onClick(v: View) {
        when (v.id) {
            R.id.tv_model -> SelectDialog.show(
                this, R.string.select.resString(),
                viewModel.examModelPosition, viewModel.models,
                REQUEST_MODEL
            )
            R.id.tv_condition -> SelectDialog.show(
                this, R.string.select.resString(),
                viewModel.examConditionPosition, viewModel.conditions,
                REQUEST_CONDITION
            )
            R.id.tv_save -> {
                val isCheck = binding.rgCheck.checkedRadioButtonId == R.id.rb_yes0
                val isResit = binding.rgResit.checkedRadioButtonId == R.id.rb_yes1
                val isTrip = binding.rgTrip.checkedRadioButtonId == R.id.rb_yes2
                val isSave = binding.rgSave.checkedRadioButtonId == R.id.rb_yes3
                viewModel.save(isCheck, isResit, isTrip, isSave)
                R.string.saveSuccess.showToast()
                finish()
            }
        }
    }
}